
import usePage from "@/hooks/page/usePage";
import { useAxiosRef } from "@/utils/request";
import { computed, unref } from "vue";
import { AxiosRequestConfig } from 'axios';

export function usePageList(axiosConf: AxiosRequestConfig) {
    const _modelForm = {};

    const {
        reload,
        axiosRef,
    } = useAxiosRef(axiosConf);

    //分页
    const { pagination, changePage, setPageChangeFunc } = usePage();

    pagination.total = computed(() => unref(axiosRef).data.count);
    const resultDatas = computed(() => unref(axiosRef).data.list || []);

    //加载
    function load() {
        reload({
            params: {
                pageNo: pagination.current,
                pageSize: pagination.pageSize,
                ..._modelForm
            },
        });
    }
    setPageChangeFunc(load)

    //查询
    function searchHeader(record: Record<string, unknown>) {
        pagination.current = 1
        Object.assign(_modelForm, record);
        load();
    }

    //获取参数
    function getModelData() {
        return _modelForm
    }

    //设置参数
    function setModelData(record: Record<string, unknown>) {
        Object.assign(_modelForm, record)
    }

    return {
        pagination, changePage, searchHeader, resultDatas, getModelData, setModelData, load
    }
}